import { zhooks } from 'ym-design'
const { useRequest } = zhooks

function editUsername(username: string) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('Failed to modify username'));
    }, 1000);
  });
}

function HookUseRequestRetry() {
  const { loading, run, data, error } = useRequest(editUsername, {
    retryCount: 3,
    retryInterval: 2000,
  });

  return (
    <div>
      <p>如果不设置retryInterval 则重新请求时间间隔根据 retryCount递增</p>
      <p style={{ color: 'red' }}>{error && error.message}</p>
      <button disabled={loading} onClick={() => run('new username')}>
        run 请求错误后，再重试3次！
      </button>
    </div>
  );
}

export default HookUseRequestRetry;